<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户中心</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <?php include 'includes/navbar.php'; ?>
    <div class="container mt-5">
        <div class="card">
            <h2 class="mb-4 text-center">用户中心</h2>
            <div id="userInfo">
                <p>用户名: <span id="username"></span></p>
                <p>余额: <span id="balance"></span></p>
                <a href="add-balance.html" class="btn btn-primary w-100">充值余额</a>
            </div>
        </div>
        <div class="card mt-5">
            <h3 class="mb-4 text-center">调用记录</h3>
            <div id="callRecords">
                <!-- 调用记录内容 -->
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="js/script.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            fetchUserInfo();
            fetchCallRecords();
        });

        function fetchUserInfo() {
            fetch('getUserInfo.php')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        document.getElementById('username').textContent = data.username;
                        document.getElementById('balance').textContent = data.balance;
                    } else {
                        document.getElementById('username').textContent = '未登录';
                        document.getElementById('balance').textContent = '0.00';
                        window.location.href = 'login.html';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }

        function fetchCallRecords() {
            fetch('get-call-records.php')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var callRecords = data.records;
                        var callRecordsHtml = '';
                        callRecords.forEach(function(record) {
                            callRecordsHtml += `
                                <div class="card mb-3">
                                    <div class="card-body">
                                        <p>工具名称: ${record.tool_name}</p>
                                        <p>调用时间: ${record.created_at}</p>
                                        <button type="button" class="btn btn-primary" onclick="showCallResult(${record.id})">查看结果</button>
                                    </div>
                                </div>
                            `;
                        });
                        document.getElementById('callRecords').innerHTML = callRecordsHtml;
                    } else {
                        document.getElementById('callRecords').innerHTML = '<p>暂无调用记录</p>';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }

        function showCallResult(recordId) {
            fetch('get-call-result.php?id=' + recordId)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        var resultHtml = '<div class="alert alert-success" role="alert">';
                        resultHtml += '<p>调用结果: ' + data.result + '</p>';
                        resultHtml += '</div>';
                        document.getElementById('callResult').innerHTML = resultHtml;
                        $('#callResultModal').modal('show');
                    } else {
                        document.getElementById('callResult').innerHTML = '<div class="alert alert-danger" role="alert">' + data.message + '</div>';
                    }
                })
                .catch(error => {
                    document.getElementById('callResult').innerHTML = '<div class="alert alert-danger" role="alert">请求失败: ' + error.message + '</div>';
                });
        }
    </script>
</body>
</html>